// pages/voucherRecive/voucherRecive.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresslist:[],
    color:'#A3A4AA',
    name:'',
    phone:'',
    region:[],
    detailAddress:'',
    uid:'',
    orderId:'',
    skuCode:'',
    skuName:'',
    createTime:''
  },
  RegionChange: function(e) {
    this.setData({
      region: e.detail.value,
      color:'#282828'
    })
  },
  formSubmit(e){
    let data=e.detail.value;
    if(data.name==''){
      wx.showToast({
        title: "请输入收件人",
        icon: 'none',
      });
      return ;
    }
    let regex = /^1[3456789]\d{9}$/;
    if(!regex.test(data.phone)){
      wx.showToast({
        title: "请输入正确的手机号码",
        icon: 'none',
      });
      return ;
    }
    if(this.data.region.length==0){
      wx.showToast({
        title: "请选择所在地区",
        icon: 'none',
      });
      return ;
    }
    if(data.detailAddress==''){
      wx.showToast({
        title: "请输入详细地址",
        icon: 'none',
      });
      return ;
    }
    let province=this.data.region[0];
    let city=this.data.region[1];
    let county=this.data.region[2];
    let _this=this;
    wx.request({
      url: app.globalData.onlineUrl+'/consignee/add', 
      method: 'POST',
      data: {
        province:province,
        city:city,
        phone:data.phone,
        county:county,
        detailAddress:data.detailAddress,
        name:data.name,
        uid:_this.data.uid
      },
      success: function(result) {   
        console.log(result.data.data.insertId)     
        let id=result.data.data.insertId;
        let currentAddress=province+''+city+''+county+''+data.detailAddress;
        let consigneePhone=data.phone;
        let consigneeName=data.name;
        let orderId=_this.data.orderId;
        console.log(1234567,orderId)     
        console.log(1234567,currentAddress)     
        wx.request({
          url:app.globalData.onlineUrl+"/redemption/receive",
          header:{'content-type': 'application/json'},
          method:"POST",
          data:{consigneeId:id,consigneeAddress:currentAddress,consigneePhone:consigneePhone,consigneeName:consigneeName,id:orderId},
          dataType:"json",
          complete:function(result) {
            wx.showToast({
              title: "领取成功",
              icon: 'success',
            });
            _this.authorizationBtn();
            
          }
        })
      },
      fail: function(err) {
      },
      complete: function() {}
    });
  
    
  },

    // // 是否设置过授权
    // openPopup() {
    //   console.log(123);
    //   const _this = this;
    //   // 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
    //   wx.getSetting({
    //     withSubscriptions: true, // 是否获取用户订阅消息的订阅状态，默认false不返回
    //     success(res) {
    //       console.log('res.authSetting', res.authSetting)
    //       if (res.authSetting['scope.subscribeMessage']) {
    //         console.log('用户点击了“总是保持以上，不再询问”')
    //         _this.authorizationBtn();
    //       } else {
    //         console.log('用户点击“总是保持以上，不再询问”则每次都会调起订阅消息')
    //         //因为没有选择总是保持，所以需要调起授权弹窗再次授权
    //           _this.authorizationBtn();
    //       }
    //       wx.switchTab({
    //         url: '/pages/order/order'
    //       })
    //     },
    //   })
    // },
  
      // 授权
    authorizationBtn() {
      wx.requestSubscribeMessage({
        tmplIds: ['9cfuuyH99tleq365J0uTFoOEdHOk-Kldi-kZoKzr7mI'],
        success(res) {
          console.log('授权成功',res)
          wx.switchTab({
            url: '/pages/order/order'
          })
        }
      })
    },  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      uid:wx.getStorageSync('uid'),
      orderId:options.orderId,
      skuCode:options.skuCode,
      skuName:options.skuName,
      createTime:options.createTime
    })
    console.log(this.data.orderId,this.data.skuCode);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})